function AddDependancy() {
  var entity_key = $('#entity-key').attr('value');
  var data = 'target=' + $('#new-depend').attr('value') + '&func=add_depend';
  data += '&id=' + entity_key + '&type=' + $('#entity-type').attr('value');
  $.ajax({
      type: 'post',
       url: '/rpc',
      data: data,
   success: function(responseText) {
     
     window.location.replace(window.location.pathname + '?id=' + entity_key);
   }
 });

}

function CheckSystems(systems) {
 for(i in systems) {
   key = systems[i];
   data = 'key=' + key + '&type=system&func=check';
   $.ajax({
        type: 'post',
         url: '/rpc',
        data: data,
     success: function(responseText) {
          info = JSON.parse(responseText);
          var span_el = document.getElementById(info.key);
          span_el.innerHTML = info.status;
          switch(info.status) {
            case 'OK':
              $('#' + info.key).attr('style','font-weight: bold;color:green');
              break;
            case 'FAILURE':
              $('#' + info.key).attr('style','font-weight: bold;color:red');
              break;
            default:
              $('#' + info.key).attr('style','color:yellow');
              span_el.innerHTML = 'UNKNOWN';
         }        
     }
   });
 }
}

function EditSystem(system_key) {
  var data = 'type=system&func=info&key=' + system_key;
  $.ajax({
       data: data,
       type: 'post',
        url: '/rpc',
    success: function(responseText) {
       var info = JSON.parse(responseText);
       $('#sys-key').attr('value',info.key);
       $('#sys-name').attr('value',info.name);
       $('#sys-url').attr('value',info.url);
       $('#sys-repo').attr('value',info.repo);
       $('#sys-dlg').dialog("option","title","Update " + info.name);
       $('#sys-dlg').dialog('open');
    }
   });
}


function RemoveDependancy(target_key) {
  var entity_key = $('#entity-key').attr('value');
  var data = 'target=' + target_key + '&func=rm_depend';
  data += '&id=' + entity_key + '&type=' + $('#entity-type').attr('value');
  $.ajax({
      type: 'post',
       url: '/rpc',
      data: data,
   success: function(responseText) {
    window.location.replace(window.location.pathname + '?id=' + entity_key);
   }
 });
}

function RemoveTask(target_key) {
  var entity_key = $('#entity-key').attr('value');
  var data = 'target=' + target_key + '&func=rm_task';
  data += '&id=' + entity_key + '&type=' + $('#entity-type').attr('value');
  $.ajax({
      type: 'post',
       url: '/rpc',
      data: data,
   success: function(responseText) {
    window.location.replace(window.location.pathname + '?id=' + entity_key);
   }
 });
}



function SaveSystem() {
  var data = 'type=system&func=save&key=' + $('#sys-key').attr('value');
  data += '&name=' + $('#sys-name').attr('value');
  data += '&url=' + $('#sys-url').attr('value');
  data += '&repo=' + $('#sys-repo').attr('value');
  $('#sys-dlg').dialog('close');
  $.ajax({
       type: 'post',
       data: data,
        url: '/rpc',
    success: function(responseText) {
      window.location.replace(window.location.pathname);
    }
  });

}


function SetAsDependency() {
  var data = 'type=project&func=set_depend&id=' + $('#entity-key').attr('value');
  data += '&target=' + $('#target-depend').attr('value');
  $.ajax({
       type: 'post',
       data: data,
        url: '/rpc',
    success: function(responseText) {
         $('#set-depend-dlg').dialog('close'); 
         alert(responseText);
       
    }
  });
}

function Update(input) {
  var entity_key = $('#entity-key').attr('value');
  var data = 'value=' + $('#' + input).attr('value') + '&name=' + input + '&func=update';
  data += '&id=' + entity_key + '&type=' + $('#entity-type').attr('value');
  $.ajax({
      type: 'post',
       url: '/rpc',
      data: data,
   success: function(responseText) {
         //alert(responseText);
     window.location.replace(window.location.pathname + '?id=' + entity_key);
   }
 });
}

function test() {
 alert('In test');
}
